#include <wx/wx.h>
#include "wx/socket.h"


class MyApp : public wxApp
{
	virtual bool OnInit();
};

IMPLEMENT_APP(MyApp)


class MyFrame : public wxFrame
{
public:
	~MyFrame()
	{
		delete m_server;
	}
	MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
	void OnQuit(wxCommandEvent& event);
	void OnAbout(wxCommandEvent& event);
	void OnServerEvent(wxSocketEvent &event);
	void OnSocketEvent(wxSocketEvent &event);
private:
	wxSocketServer *m_server;
};

// IDs for the controls and the menu commands
enum
{
	ID_Quit=1,
	ID_About,

	// id for sockets
	ID_Server,
	ID_Socket
};


bool MyApp::OnInit()
{
	MyFrame *frame = new MyFrame(_T("Hello World"), wxPoint(50,50),
                wxSize(450,350));

	frame->Connect( ID_Quit, wxEVT_COMMAND_MENU_SELECTED,
		(wxObjectEventFunction) &MyFrame::OnQuit );
	frame->Connect( ID_About, wxEVT_COMMAND_MENU_SELECTED,
		(wxObjectEventFunction) &MyFrame::OnAbout );
	frame->Connect( ID_Server, wxEVT_SOCKET,
		(wxObjectEventFunction) &MyFrame::OnServerEvent);
	frame->Connect( ID_Socket, wxEVT_SOCKET,
		(wxObjectEventFunction) &MyFrame::OnSocketEvent);

	frame->Show(TRUE);
	SetTopWindow(frame);
	return TRUE;
}

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
	: wxFrame((wxFrame*)NULL,-1,title,pos,size)
{
	// create menubar
	wxMenuBar *menuBar = new wxMenuBar;
	// create menu
	wxMenu *menuFile = new wxMenu;
	// append menu entries
	menuFile->Append(ID_About,_T("&About..."));
	menuFile->AppendSeparator();
	menuFile->Append(ID_Quit,_T("E&xit"));
	// append menu to menubar
	menuBar->Append(menuFile,_T("&File"));
	// set frame menubar
	SetMenuBar(menuBar);

	// create frame statusbar
	CreateStatusBar();
	// set statusbar text
	SetStatusText(_T("Welcome to wxWindows!"));

	// Create the address - defaults to localhost:0 initially
	wxIPV4address addr;
	addr.Service(3000);
	// Create the socket
	m_server = new wxSocketServer(addr);
  	if (m_server->Ok()) {
		wxMessageBox(_T("wxWindows Hello World example."),_T("Server listening"),
        	        wxOK|wxICON_INFORMATION, this);
		// Setup the event handler and subscribe to connection events
		m_server->SetEventHandler(*this, ID_Server);
		m_server->SetNotify(wxSOCKET_CONNECTION_FLAG);
		m_server->Notify(true);
	}
}

void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
	Close(TRUE);
}

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
	wxMessageBox(_T("wxWindows Hello World example."),_T("About Hello World"),
                wxOK|wxICON_INFORMATION, this);
}

void MyFrame::OnServerEvent(wxSocketEvent &event)
{
	wxString s = _("OnServerEvent: ");
	switch(event.GetSocketEvent())
	{
		case wxSOCKET_CONNECTION:
			s.Append(_("wxSOCKET_CONNECTION\n"));
			break;
		default:
			s.Append(_("Unexpected event !\n"));
			break;
	}
	// Accept new connection if there is one in the pending
	// connections queue, else exit. We use Accept(false) for
	// non-blocking accept (although if we got here, there
	// should ALWAYS be a pending connection).
	wxSocketBase *sock = m_server->Accept(false);
  	if (sock) {
		wxMessageBox(_T("wxWindows Hello World example."),_T("Accept connection"),
        	        wxOK|wxICON_INFORMATION, this);
		sock->SetEventHandler(*this, ID_Socket);
		sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
		sock->Notify(true);
	}

}
void MyFrame::OnSocketEvent(wxSocketEvent &event)
{
	wxString s = _("OnSocketEvent: ");
	wxSocketBase *sock = event.GetSocket();

	// First, print a message
	switch(event.GetSocketEvent())
	{
		case wxSOCKET_INPUT:
			s.Append(_("wxSOCKET_INPUT\n"));
			break;
		case wxSOCKET_LOST:
			s.Append(_("wxSOCKET_LOST\n"));
			break;
		default:
			s.Append(_("Unexpected event !\n"));
			break;
	}
}

